$(function() {
    function navSlider() {
        var $nav = $('.nav'),
            $cur = $('.nav li.cur a'),
            $navLine = $('.nav-line'),
            $anchor = $('a', $nav.children()),
            curPosL = $cur.position().left,
            curW = $cur.outerWidth(true),
            curIdx = $('li.cur', $nav).index();
        $navLine.css({
            'width': curW,
            'left': curPosL
        });
        $anchor.not('li.last a', $nav).each(function(index) {
            var posL = $(this).position().left,
                w = $(this).outerWidth(true);
            $(this).mouseenter(function() {
                $navLine.animate({
                    'width': w,
                    'left': posL
                }, 250);
                $(this).parent().addClass('cur').siblings().removeClass('cur');
            });
        });
        $nav.mouseleave(function() {
            $navLine.animate({
                'width': curW,
                'left': curPosL
            }, 250);
            $anchor.parent(':eq(' + curIdx + ')').addClass('cur').siblings().removeClass('cur');
        });
    };
    navSlider();
    // sideSlider
    function sideSlider() {
        if (!$(".help-side dl").length) {
            return false;
        }

        var $aCur = $(".help-side dl").find(".cur a"),
            $targetA = $(".help-side dl dd a"),
            $sideSilder = $(".side-slider"),
            curT = $aCur.position().top - 3;

        $sideSilder.stop(true, true).animate({
            "top": curT
        });

        $targetA.mouseenter(function() {
            var posT = $(this).position().top - 3;
            $sideSilder.stop(true, true).animate({
                "top": posT
            }, 240);
        }).parents(".help-side").mouseleave(function(_curT) {
            _curT = curT
            $sideSilder.stop(true, true).animate({
                "top": _curT
            });
        });
    };
    sideSlider();
    $(".J_add").click(function() {
        var this_html = "<td align=\"right\"> <strong>关键词：</strong>\
                            </td>\
                            <td>\
                                <input type=\"text\" name=\"keywords[]\" class=\"validate[required,length[2,20]]] input\" style=\"width:140px;\"></td>\
                            <td align=\"right\"> <strong>价格：(元/天)</strong></td>\
                            <td>\
                                <input type=\"text\" name=\"moneys[]\" class=\"validate[required,custom[zhengshu]] input\"></td>\
                            <td>\
                                <div class=\"J_del remove ico ico_application_form_delete\" ></div>\
                            </td>";
        var after_tr = $("#keywords").append("<tr class='after_tr'></tr>"); //向当前行插入一行tr
        var after_tr = $(".after_tr:last"); //选择插入行的类名
        after_tr.html(this_html);
        after_tr.validationEngine();
    });
    //移除方法
    $(".J_del").live("click", function() {
        $(this).parent().parent().remove();
    });
    $('#main_left').css('height', $("#main_right").height() + 10);
});
$(window).resize(function() {
    $('#main_left').css('height', $("#main_right").height() + 10);
});
(function($){
    $.fn.extend({
        sSelect: function() {
            return this.each(function(i,obj){
                var selectId = (this.name||this.id)+'__jQSelect'+i||'__jQSelect'+i;
                if(obj.style.display != 'none' && $(this).parents()[0].id.indexOf('__jQSelect')<0){
                    var tabindex = this.tabIndex||0;
                    $(this).before("<div class='dropdown' id="+selectId+" tabIndex="+tabindex+"></div>").prependTo($("#"+selectId));
                    var selectZindex = $(this).css('z-index'),selectIndex = $('#'+selectId+' option').index($('#'+selectId+' option:selected')[0]);
                    $('#'+selectId).append('<div class="dropselectbox"><h4></h4><ul></ul></div>');
                    $('#'+selectId+' h4').empty().append($('#'+selectId+' option:selected').text());
                    var selectWidth=$('#'+selectId+' select').width();
                    if($.browser.safari){selectWidth = selectWidth+15}
                    $('#'+selectId+' h4').css({width:selectWidth});
                    var selectUlwidth = selectWidth + parseInt($('#'+selectId+' h4').css("padding-left")) + parseInt($('#'+selectId+' h4').css("padding-right"));
                    $('#'+selectId+' ul').css({width:selectUlwidth+'px'});
                    $('#'+selectId+' select').hide();
                    $('#'+selectId+' div').hover(function(){
                        $('#'+selectId+' h4').addClass("over");
                    },function(){
                        $('#'+selectId+' h4').removeClass("over");
                    });

                    var timeobj;
                    $('#'+selectId+' ul').bind("mouseover",function(e){
                        clearTimeout(timeobj);
                    });
                    var click_fun =function(){
                        $('#'+selectId+' h4').addClass("current");
                        $('#'+selectId+' ul').show();
                        var selectZindex = $('#'+selectId).css('z-index');
                        if ($.browser.msie || $.browser.opera){$('.dropdown').css({'position':'relative','z-index':'0'});}
                        $('#'+selectId).css({'position':'relative','z-index':'999'});
                        $.fn.setSelectValue(selectId);
                        selectIndex = $('#'+selectId+' li').index($('.selectedli')[0]);
                        var windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $('#'+selectId).offset().top;
                        var ulspace = $('#'+selectId+' ul').outerHeight(true);
                        var windowspace2 = $('#'+selectId).offset().top - $(window).scrollTop() - ulspace;
                        windowspace < ulspace && windowspace2 > 0?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
                        $(window).scroll(function(){
                            windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $('#'+selectId).offset().top;
                            windowspace < ulspace?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
                        });
                        $('#'+selectId+' li').click(function(e){
                            selectIndex = $('#'+selectId+' li').index(this);
                            $.fn.keyDown(selectId,selectIndex);
                            $('#'+selectId+' h4').empty().append($('#'+selectId+' option:selected').text());
                            $.fn.clearSelectMenu(selectId,selectZindex);
                            e.stopPropagation();
                            e.cancelbubble = true;
                        })
                            .hover(
                            function(){
                                $('#'+selectId+' li').removeClass("over");
                                $(this).addClass("over").addClass("selectedli");
                                selectIndex = $('#'+selectId+' li').index(this);
                            },
                            function(){
                                $(this).removeClass("over");
                            }
                        );
                    }

                    $('#'+selectId)
                        .bind("focus",function(){
                            //$.fn.clearSelectMenu(selectId,selectZindex);
                            $('#'+selectId+' h4').addClass("over");
                        })
                        .bind("click",function(e){
                            if($('#'+selectId+' ul').css("display") == 'block'){
                                $.fn.clearSelectMenu(selectId,selectZindex);
                                return false;
                            }else{
                                click_fun();
                            };
                            e.stopPropagation();
                        })
                        .bind("mouseover",function(e){
                            if($('#'+selectId+' ul').css("display") == 'block'){
                                //$.fn.clearSelectMenu(selectId,selectZindex);
                                return false;
                            }else{
                                click_fun();
                            };
                            e.stopPropagation();
                        })
                        .bind("mouseout",function(e){
                            if($('#'+selectId+' ul').css("display") == 'block'){
                                timeobj = setTimeout(function(){
                                    $.fn.clearSelectMenu(selectId,selectZindex);
                                },500);
                                return false;
                            }
                            e.stopPropagation();
                        })
                        .bind('mousewheel', function(e,delta) {
                            e.preventDefault();
                            var mousewheel = {
                                $obj : $('#'+selectId+' li.over'),
                                $slength : $('#'+selectId+' option').length,
                                mup:function(){
                                    this.$obj.removeClass("over");
                                    selectIndex == 0?selectIndex = 0:selectIndex--;
                                    $.fn.keyDown(selectId,selectIndex);
                                },
                                mdown:function(){
                                    this.$obj.removeClass("over");
                                    selectIndex == (this.$slength - 1)?selectIndex = this.$slength - 1:selectIndex ++;
                                    $.fn.keyDown(selectId,selectIndex);
                                }
                            }
                            delta>0?mousewheel.mup():mousewheel.mdown();
                        })
                        .bind("dblclick", function(){
                            $.fn.clearSelectMenu(selectId,selectZindex);
                            return false;
                        })
                        .bind("keydown",function(e){
                            $(this).bind('keydown',function(e){
                                if (e.keyCode == 40 || e.keyCode == 38 || e.keyCode == 35 || e.keyCode == 36){
                                    return false;
                                }
                            });
                            var $obj = $('#'+selectId+' li.over'),$slength = $('#'+selectId+' option').length;
                            switch(e.keyCode){
                                case 9:
                                    return true;
                                    break;
                                case 13:
                                    //enter
                                    $.fn.clearSelectMenu(selectId,selectZindex);
                                    break;
                                case 27:
                                    //esc
                                    $.fn.clearSelectMenu(selectId,selectZindex);
                                    break;
                                case 33:
                                    $obj.removeClass("over");
                                    selectIndex = 0;
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                case 34:
                                    $obj.removeClass("over");
                                    selectIndex = ($slength - 1);
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                case 35:
                                    $obj.removeClass("over");
                                    selectIndex = ($slength - 1);
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                case 36:
                                    $obj.removeClass("over");
                                    selectIndex = 0;
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                case 38:
                                    //up
                                    e.preventDefault();
                                    $obj.removeClass("over");
                                    selectIndex == 0?selectIndex = 0:selectIndex--;
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                case 40:
                                    //down
                                    e.preventDefault();
                                    $obj.removeClass("over");
                                    selectIndex == ($slength - 1)?selectIndex = $slength - 1:selectIndex ++;
                                    $.fn.keyDown(selectId,selectIndex);
                                    break;
                                default:
                                    e.preventDefault();
                                    break;
                            };
                        })
                        .bind("blur",function(){
                            $.fn.clearSelectMenu(selectId,selectZindex);
                            return false;
                        })
                        .bind("selectstart",function(){
                            return false;
                        });
                }else if($(this).parents()[0].id.indexOf('__jQSelect')>0){
                    selectId = $(this).parents()[0].id;
                    $.fn.setSelectValue(selectId);
                    var selectWidth=$('#'+selectId+' select').width();
                    if($.browser.safari){selectWidth = selectWidth+15}
                    $('#'+selectId+' h4').css({width:selectWidth});
                    var selectUlwidth = selectWidth + parseInt($('#'+selectId+' h4').css("padding-left")) + parseInt($('#'+selectId+' h4').css("padding-right"));
                    $('#'+selectId+' ul').css({width:selectUlwidth+'px'});
                    if(this.style.display != 'none'){$(this).hide();}
                }})},
        clearSelectMenu:function(selectId,selectZindex){
            if(selectId != undefined){
                selectZindex = selectZindex||'auto';
                $('#'+selectId+' ul').empty().hide();
                $('#'+selectId+' h4').removeClass("over").removeClass("current");
                $('#'+selectId).css({'z-index':selectZindex});
            }
        },
        setSelectValue:function(sID){
            var content = [];
            $.each($('#'+sID+' option'), function(i){
                content.push("<li class='FixSelectBrowser'>"+$(this).text()+"</li>");
            });
            content = content.join('');
            $('#'+sID+' ul').html(content);
            $('#'+sID+' h4').html($('#'+sID+' option:selected').text());
            $('#'+sID+' li').eq($('#'+sID+' select')[0].selectedIndex).addClass("over").addClass("selectedli");
        },
        keyDown:function(sID,selectIndex){
            var $obj = $('#'+sID+' select');
            $obj[0].selectedIndex = selectIndex;
            $obj.change();
            $('#'+sID+' li:eq('+selectIndex+')').toggleClass("over");
            $('#'+sID+' h4').html($('#'+sID+' option:selected').text());
        }
    });
    var types = ['DOMMouseScroll', 'mousewheel'];
    $.event.special.mousewheel = {
        setup: function() {
            if ( this.addEventListener )
                for ( var i=types.length; i; )
                    this.addEventListener( types[--i], handler, false );
            else
                this.onmousewheel = handler;
        },
        teardown: function() {
            if ( this.removeEventListener )
                for ( var i=types.length; i; )
                    this.removeEventListener( types[--i], handler, false );
            else
                this.onmousewheel = null;
        }
    };
    $.fn.extend({
        mousewheel: function(fn) {
            return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
        },

        unmousewheel: function(fn) {
            return this.unbind("mousewheel", fn);
        }
    });
    function handler(event) {
        var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
        event = $.event.fix(event || window.event);
        event.type = "mousewheel";
        if ( event.wheelDelta ) delta = event.wheelDelta/120;
        if ( event.detail     ) delta = -event.detail/3;
        args.unshift(event, delta);
        return $.event.handle.apply(this, args);
    }
})(jQuery);

function formatdate(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
};

function msgok(str) {
    ZENG.msgbox.show(str, 4, 2000);
}

function msgerr(str) {
    ZENG.msgbox.show(str, 5, 2000);
}

function msgwarn(str) {
    ZENG.msgbox.show(str, 1, 2000);
}

function msgload(str) {
    ZENG.msgbox.show(str, 6, 99999)
}

function msghide() {
    ZENG.msgbox.hide();
}

function exit() {
    art.dialog({
        content: '您确定要退出系统吗？',
        icon: 'question',
        lock: true,
        ok: function() {
            window.location.href = 'http://' + window.location.host + '/Login/logout';
        },
        cancelVal: '取消',
        cancel: true
    });
}

function jingzhun() {
    var zTreeObj = $.fn.zTree.getZTreeObj("jzval");
    var nodes = zTreeObj.getNodes();
    var ids = "";
    var pids = "";
    var childNodes = zTreeObj.transformToArray(nodes);
    //console.log(childNodes);
    for (var j = 0; j < childNodes.length; j++) {
        if (childNodes[j].check_Child_State != 0) {
            ids += childNodes[j].id + ",";
        }
    }
    //console.log(ids);
    ids = ids.substring(0, ids.length - 1);
    $("#jingzhunci").val(ids);
}

function diqu() {
    var zTreeObj = $.fn.zTree.getZTreeObj("dqval");
    var nodes = zTreeObj.getNodes();
    var ids = "";
    var pids = "";
    var childNodes = zTreeObj.transformToArray(nodes);
    //console.log(childNodes);
    for (var j = 0; j < childNodes.length; j++) {
        if (childNodes[j].check_Child_State != 0) {
            ids += childNodes[j].id + ",";
        }
    }
    //console.log(ids);
    ids = ids.substring(0, ids.length - 1);
    $("#district").val(ids);
}

function validateForm(containerId,ajaxurl) {
    var valid = $(containerId).validationEngine({
        returnIsValid: true,
        showOnMouseOver: false
    });
    if (valid == true) {
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: $(containerId).serialize(),
            cache: false,
            dataType: "json",
            success: function(data) {
                if (data.status == "true") {
                    if(data.Msg!=null){
                        msgok(data.Msg);
                    }
                    if(data.url!=null){
                        setTimeout( function(){
                            window.location=data.url;
                        },100);
                    }
                    if(data.next=='two'){
                        one();
                    }
                    if(data.next=='three'){
                        two();
                    }
                } else {
                    msgwarn(data.Msg);
                }
            }
        });
    } else {
        msgerr('表单填写不正确，请按要求填写！');
    }
}

    function del(url,id){
        art.dialog({
            content: '您确定要删除吗？',
            icon: 'question',
            lock: true,
            ok: function() {
                $.ajax({
                    url:url,
                    type:"POST",
                    data:{"userId":id},
                    cathe:false,
                    dataType:"json",
                    success:function(data){
                        if(data.status=="true"){
                            msgok(data.Msg);
                            setTimeout(function(){parent.location.reload()},1000);                            
                        }else{
                            msgwarn(data.Msg);
                        }
                    }


                });
            },
            cancelVal: '取消',
            cancel: true
        });
    }

    function start(url,id){
        art.dialog({
            content: '您确定要开启吗？',
            icon: 'question',
            lock: true,
            ok: function() {
                $.ajax({
                    url:url,
                    type:"POST",
                    data:{"userId":id},
                    cache:false,
                    dataType:"json",
                    success:function(data){
                        if(data.status="true"){
                            msgok(data.Msg);
                            setTimeout(function(){parent.location.reload()},1000);
                        }else{
                            msgwarn(data.Msg);
                        }
                    }
                });
            },
            cancelVal: '取消',
            cancel: true
        });
    }

    function pause(url,id){
        art.dialog({
            content: '您确定要暂停吗？',
            icon: 'question',
            lock: true,
            ok: function() {
                $.ajax({
                    url:url,
                    type:"POST",
                    data:{"userId":id},
                    cache:false,
                    dataType:"json",
                    success:function(data){
                        if(data.status="true"){
                            msgok(data.Msg);
                            setTimeout(function(){parent.location.reload()},1000);
                        }else{
                            msgwarn(data.Msg);
                        }
                    }
                });
            },
            cancelVal: '取消',
            cancel: true
        });
    }    

//输入数值后转化为大写
function convertCurrency(currencyDigits) {
// Constants:
    var MAXIMUM_NUMBER = 99999999999.99;
    // Predefine the radix characters and currency symbols for output:
    var CN_ZERO = "零";
    var CN_ONE = "壹";
    var CN_TWO = "贰";
    var CN_THREE = "叁";
    var CN_FOUR = "肆";
    var CN_FIVE = "伍";
    var CN_SIX = "陆";
    var CN_SEVEN = "柒";
    var CN_EIGHT = "捌";
    var CN_NINE = "玖";
    var CN_TEN = "拾";
    var CN_HUNDRED = "佰";
    var CN_THOUSAND = "仟";
    var CN_TEN_THOUSAND = "万";
    var CN_HUNDRED_MILLION = "亿";
    var CN_SYMBOL = "人民币";
    var CN_DOLLAR = "元";
    var CN_TEN_CENT = "角";
    var CN_CENT = "分";
    var CN_INTEGER = "整";
    // Variables:
    var integral; 
    var decimal; 
    var outputCharacters; 
    var parts;
    var digits, radices, bigRadices, decimals;
    var zeroCount;
    var i, p, d;
    var quotient, modulus;

    currencyDigits = currencyDigits.toString();

    currencyDigits = currencyDigits.replace(/,/g, "");
    currencyDigits = currencyDigits.replace(/^0+/, "");

    if (Number(currencyDigits) > MAXIMUM_NUMBER) {
        msgwarn("充值金额太大");
        return false;
    }

    parts = currencyDigits.split(".");
    if (parts.length > 1) {
        integral = parts[0];
        decimal = parts[1];

        decimal = decimal.substr(0, 2);
    }
    else {
        integral = parts[0];
        decimal = "";
    }

    digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
    radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
    bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
    decimals = new Array(CN_TEN_CENT, CN_CENT);
    
    outputCharacters = "";
    
    if (Number(integral) > 0) {
        zeroCount = 0;
        for (i = 0; i < integral.length; i++) {
            p = integral.length - i - 1;
            d = integral.substr(i, 1);
            quotient = p / 4;
            modulus = p % 4;
            if (d == "0") {
                zeroCount++;
            }
            else {
                if (zeroCount > 0)
                {
                    outputCharacters += digits[0];
                }
                zeroCount = 0;
                outputCharacters += digits[Number(d)] + radices[modulus];
            }
            if (modulus == 0 && zeroCount < 4) {
                outputCharacters += bigRadices[quotient];
            }
        }
        outputCharacters += CN_DOLLAR;
    }
    
    if (decimal != "") {
        for (i = 0; i < decimal.length; i++) {
            d = decimal.substr(i, 1);
            if (d != "0") {
                outputCharacters += digits[Number(d)] + decimals[i];
            }
        }
    }
    
    if (outputCharacters == "") {
        outputCharacters = CN_ZERO + CN_DOLLAR;
    }
    if (decimal == "") {
        outputCharacters += CN_INTEGER;
    }
    outputCharacters = CN_SYMBOL + outputCharacters;
    return outputCharacters;
}